<?php
	session_start();
        
	include_once('../classes/HTMLManager.class.php');
	include_once('../classes/ArticleManager.class.php');
        
        define(ACTION_ARTICLE_READ, 'read');
	define(ACTION_ARTICLE_ARCHIVE, 'archive');
	define(ACTION_ARTICLE_ARCHIVE_CATEGORY, 'category');
	define(ACTION_ARTICLE_ARCHIVE_TAG, 'tag');
	
	$article = null;
	$articles = null;
	
	if (isset($_GET['action'])) {
		$am = new ArticleManager();
		if ($_GET['action'] == ACTION_ARTICLE_READ) {
                    if(isset($_GET['article_id'])) {
                            $article = $am->get_article_by_id($_GET['article_id']);
                            if ($article == null) {
                            }
                    }
		} else if ($_GET['action'] == ACTION_ARTICLE_ARCHIVE) { // ACTION: ARCHIVE
			
                    $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
                        
                    if (isset($_GET['archive_type'])) { // ARCHIVIO SPECIALIZZATO: TAG O CATEGORIE

                            if ($_GET['archive_type'] == ACTION_ARTICLE_ARCHIVE_CATEGORY) { // ARCHIVIO PER CATEGORIE

                                    if (isset($_GET['type_id'])) {
                                            $num_pages = $am->get_num_pages(ACTION_ARTICLE_ARCHIVE_CATEGORY, $_GET['type_id']);
                                            $articles = $am->get_articles_by_category($_GET['type_id'], $current_page);
                                            $url = "articoli/archivio/categorie/" . $_GET['type_id'];
                                    } else {
                                            echo 'Nessuna categoria impostata';
                                    }

                            } else if ($_GET['archive_type'] == ACTION_ARTICLE_ARCHIVE_TAG) { // ARCHIVIO PER TAG

                                    if (isset($_GET['type_id'])) {
                                            $num_pages = $am->get_num_pages(ACTION_ARTICLE_ARCHIVE_TAG, $_GET['type_id']);
                                            $articles = $am->get_articles_by_tag($_GET['type_id'], $current_page);
                                             $url = "articoli/archivio/tag/" . $_GET['type_id'];
                                    } else {
                                            echo 'Nessun tag impostato';
                                    }
                                    

                            }
                    } else { //ARCHIVIO GENERALE: TUTTI GLI ARTICOLI
                            $num_pages = $am->get_archive_num_pages();
                            $articles = $am->get_all_articles($current_page);
                            $url = "articoli/archivio";
                    }
			
		}
        }

	include_once('article.view.php');
?>